"""
--------------------------------------
FileName: work.1.py
Content: 
        验证ch2.work2算法的正确性

Date:    25/09/15
Auther:  杨则瑞
Encode:  utf-8 | CRLF
--------------------------------------
"""



import csv
import subprocess
import io
import os
import sys
import random

from math import sqrt
from math import floor

failCnt = 0



def generate_test_case():

    num  = random.randint(0,10000)
    
    if num < 1000: 
        num = 0

    tst = []
    for i in range(0, num):
        tst.append(random.randint(-2147483648, 2147483647))


        


    # Fmting
    inpt = bytes()
    inpt += (bytes(str(num) + '\n',encoding="ascii"))

    for i in tst:
        inpt += (bytes(str(i) + '\n',encoding="ascii"))

    tst.reverse()

    return inpt, tst







def get_shorter(s : str):
    if len(s) < 23:
        return s
    return "{}...{}".format(s[:10],s[-10:])

for case in range(0,10):

    sys.stdout.write("Testing case %d " % case)
    
    target = (sys.argv[1])

    handler = subprocess.Popen(target,
                           stdin  = subprocess.PIPE,
                           stdout = subprocess.PIPE,
                           stderr = subprocess.STDOUT)

    
    if not handler.stdin.writable:

        print("Communication Error.")
        exit(1)
    if not handler.stdout.readable:
        print("Communication Error.")
        exit(2)

    flag : bool = True
    



    inpt , oue = generate_test_case() 


    # 获取输出
    out, _ = handler.communicate(inpt)
    out = out.decode("ascii")
    our = eval(out)

    sys.stdout.write("\nJudging if {} equals to {} ".format(get_shorter(str(our)), get_shorter(str(oue))))
    # 判断相等
    flag = (our == oue)
    

    if(flag == True):
        print("\033[1;32m""Pass""\033[0;10m")
    else:
        failCnt += 1
        print("\033[1;31m""Fail""\033[0;10m") 
                
    
print("Result:")

if failCnt == 0:
    print("\033[1;32m""All Pass.""\033[0;10m")
else :
    print("\033[1;10m""{} Fails cases".format(failCnt))